$(function(){
	$(".mc").css("display","none");
	$('#categorys').hover(function(){$(this).children('.mc').toggle();});
	
	drawValidateCode();
	$('#MEMBER_ACCOUNT').bind('blur', validateAccount);
	$('#PASSWORD').bind('blur', validatePassword);
	$('#RE_PASSWORD').bind('blur', validateRepassword);
	$('#VALIDATE_CODE').bind('blur', validateCode);
	
	$('#sub').bind('click',subFrm);
});

function drawValidateCode() {
	var vc = new ValidateCode('/validateCode.jsp',60,18,4, 'MEMBER_REG');
	vc.applyTo('validateCode');
}

function validateAccount(){
	var account = $('#MEMBER_ACCOUNT');
	var accountTip = $('#MEMBER_ACCOUNT_TIP');
	var accountVal = $.trim(account.val());
	if(accountVal.length == 0 || accountVal.length < 4  ||accountVal.length > 20){
		accountTip.html('账号长度在4-20位字符');
		accountTip.attr('class', 'error_tip');
		return false;
	} else if(!/^[\u4E00-\u9FA5A-Za-z0-9]{4,20}$/g.test(accountVal)) {
		accountTip.html('账号有非法字符');
		accountTip.attr('class', 'error_tip');
		return false;
	} else {
		accountTip.html('');
	}
	
	accountTip.html('正在检查账号是否可用...');
	var url = '/json?action=CHECK_MEMBER_NAME_ACTION&MEMBER_ACCOUNT='+ accountVal +'&random='+Math.random();
	$.post(encodeURI(url),{},function(data){
		if(data.success){
			accountTip.html('账号可用');
			accountTip.attr('class', 'right_tip');
		}
		else{
			accountTip.html('账号已存在');
			accountTip.attr('class', 'error_tip');
		}
	},'json');
	
	return true;
}

function validatePassword(){
	var pwd = $('#PASSWORD');
	var pwdTip = $('#PASSWORD_TIP');
	var pwdVal = $.trim(pwd.val());
	if(pwdVal.length == 0 || pwdVal.length < 8 || pwdVal.length > 20){
		pwdTip.html('密码长度在8-16位字符');
		pwdTip.attr('class', 'error_tip');
		return false;
	}
	else{
		pwdTip.html('');
	}
	return true;
}

function validateRepassword(){
	var rePwd = $('#RE_PASSWORD');
	var rePwdTip = $('#RE_PASSWORD_TIP');
	var rePwdVal = $.trim(rePwd.val());
	
	var pwd = $('#PASSWORD');
	var pwdVal = $.trim(pwd.val());
	if(rePwdVal.length == 0 || rePwdVal != pwdVal){
		rePwdTip.html('两次密码不一致');
		rePwdTip.attr('class', 'error_tip');
		return false;
	}
	else{
		rePwdTip.html('');
	}
	return true;
}

function validateCode(){
	var vcode = $('#VALIDATE_CODE');
	var vcodeTip = $('#VALIDATE_CODE_TIP');
	var vcodeVal = $.trim(vcode.val());
	if(vcodeVal.length == 0){
		vcodeTip.html('请输入验证码');
		vcodeTip.attr('class', 'error_tip');
		return false;
	}
	else{
		vcodeTip.html('');
	}
	return true;
}

function validateAgree(){
	var flag = $("#agree").attr("checked");
	if(!flag){
		if(confirm("如果不同意金麟条款，则不能进行注册，点击确定表示同意，点击取消将退出注册")){
			return true;
		}
		else{
			location.href = "../index.html";
			return false;
		}
	}
	return true;
}

function validateResult(){
	return validateAccount() && validatePassword() && validateRepassword() && validateCode() && validateAgree();
}

function subFrm(){
	var options = {
	    dataType: 'json',
	    beforeSubmit: validateResult,
	    success: function(data) {
	    	if(data.success){
	      		window.location.href="register02.html?name="+$('#MEMBER_ACCOUNT').val();
	      	}else{
	      		alert(data.errors.errmsg);
	      		drawValidateCode();
	      	}
	   	}
	};
	$.ajaxSetup({contentType:'application/x-www-form-urlencoded;charset=utf-8'});
    $('#frm').ajaxSubmit(options);
}
